﻿using Models;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Http;
using wnet.admin.Models.dto;
using wnet.admin.wentcommon;




namespace wnet.admin.Controllers.pcapi
{

    [Route("business/common/{action}")]
    public class commonController : ApiController
    {

        private readonly ISqlSugarClient _sqlSugarClient;
        public commonController()
        {
            _sqlSugarClient = SqlSugarConfig.CreateClient();
        } 
        /// <summary>
        /// 存储文件
        /// </summary>
        /// <returns></returns>
        //[TokenAuth]
        [HttpPost] 
        public object UploadFile()
        {
            string imgurl = ConfigurationManager.AppSettings["imgurl"];
            var file = HttpContext.Current.Request.Files[0];  //获取文件对象
            string fileName = file.FileName;  //原文件名
            string fileExt = fileName.Substring(fileName.LastIndexOf("."));//获取扩展名
                                                                           //检测上传文件是否合法

            string newFileName = DateTime.Now.ToString("yyyyMMddHHmmssffff") + fileExt; //随机生成新的文件名
            string fileFolder = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "upload");  //生成新的文件路径

            if (!Directory.Exists(fileFolder))
            {
                Directory.CreateDirectory(fileFolder);
            }


            string path = Path.Combine(fileFolder, newFileName);  //生成新的文件路径
            file.SaveAs(path);  //保存文件

            var data = new
            {
                code = 200,
                msg = "成功",
                data = new
                {
                    url = $"{imgurl}/upload/{newFileName}",
                    fileName = fileName,
                    fileId = fileName
                }
            };

            return Json(data, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });


        }





    }
}
